package com.example.smartall.fragment.JobHunting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.adapter.PervasiveAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.HospitalCategoryBean;
import com.example.smartall.entity.JobAdvertiseBean;
import com.example.smartall.entity.JobBean;
import com.example.smartall.fragment.base.BaseFragment;
import com.example.smartall.ui.JobAdvertiseDetailsActivity;
import com.example.smartall.ui.LoginActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.example.smartall.utils.TextUtils;
import com.example.smartall.view.SListView;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author: abbas
 * @CreateDate: 2022/2/2
 * @Description: 找工作模块 找工作
 */
public class JobHuntingFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Banner banner;
    private RecyclerView rvHot;
    private EditText etSearch;
    private Button btSearch;
    private SListView lvAdv;

    private List<JobAdvertiseBean.RowsBean> advBean = new ArrayList<>();
    private ListAdapter advAdapter;

    private MHandler handler;

    public JobHuntingFragment() {
        // Required empty public constructor
    }

    public static JobHuntingFragment newInstance(String param1, String param2) {
        JobHuntingFragment fragment = new JobHuntingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler = new MHandler();

        View view = inflater.inflate(R.layout.fragment_job_hunting, container, false);
        init(view);
        initEvent();
        getAdvList(null);
        return view;
    }

    @Override
    public void onResume() {
        if (advBean != null && advAdapter != null){
            lvAdv.setAdapter(advAdapter);
            advAdapter.notifyDataSetChanged();
        }else {
            getAdvList(null);
        }
        super.onResume();
    }

    protected void init(View view) {
        banner = (Banner) view.findViewById(R.id.banner);
        rvHot = (RecyclerView) view.findViewById(R.id.rv_hot);
        etSearch = (EditText) view.findViewById(R.id.et_search);
        btSearch = (Button) view.findViewById(R.id.bt_search);
        lvAdv = (SListView) view.findViewById(R.id.lv_adv);

        etSearch.setHint("职位ID");
    }


    protected void initEvent() {
        lvAdv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //TODO 职位列表条目监听 这文档实在是吐了
                /**
                 * 这里这个传递的查询详情的主键id不知道应该是哪个数据
                 * 试过另一个id 报错了找不到
                 * 但是传这个数据，详情页面数据又牛头不对马嘴
                 */
                Intent intent = new Intent(getActivity(),JobAdvertiseDetailsActivity.class);
                intent.putExtra("id",advBean.get(i).getId());
                startActivity(intent);
            }
        });

        btSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String target = etSearch.getText().toString();
                if (!TextUtils.isEmpty(target)) {
                    getAdvList(target);
                } else {
                    showToast("请先输入职位ID");
                }
            }
        });
    }

    /**
     * @Author: abbas
     * @CreateDate: 2022/2/5
     * @Description: 回来一看应该是文档又有问题
     * 不应该是职业列表，应该是招聘列表，然后显示招聘职位...
     * 烦死了
     */
    private void getAdvList(String professionId) {
        String hasParams = "";
        if (!TextUtils.isEmpty(professionId)) {
            hasParams += "?professionId=" + professionId;
        }
        Api.config(ApiConfig.JOB_LIST,null).getRequest(getActivity(), new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                JobAdvertiseBean bean = new Gson().fromJson(res, JobAdvertiseBean.class);
                if (bean != null && bean.getCode() == 200) {
                    advBean = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },hasParams);
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    if (advAdapter == null){
                        advAdapter = new ListAdapter(advBean);
                        lvAdv.setAdapter(advAdapter);
                    }else {
                        advAdapter.setData(advBean);
                    }
                    break;
            }
        }
    }

    class ListAdapter extends PervasiveAdapter<JobAdvertiseBean.RowsBean> {

        public ListAdapter(List<JobAdvertiseBean.RowsBean> datas) {
            super(datas);
        }

        @Override
        protected void setUI(ViewHolder holder, int position, Context context) {
            ImageView img = (ImageView) holder.getItemView(R.id.img);
            TextView title = (TextView) holder.getItemView(R.id.title);
            TextView subtitle = (TextView) holder.getItemView(R.id.subtitle);
            TextView content = (TextView) holder.getItemView(R.id.content);

            title.setText(advBean.get(position).getProfessionName());
            subtitle.setText(advBean.get(position).getObligation());
            content.setText(advBean.get(position).getNeed());

        }

        @Override
        protected int getLayoutId() {
            return R.layout.common_list_item;
        }
    }
}